// pages/mine/son/order/order.js.js
import request from '../../../../utils/request'
Page({

  /**
   * 页面的初始数据
   */
  data: {
    num: 0,
    list: [{
        title: '待付款',
        id: 0
      },
      {
        title: '已付款',
        id: 1
      },
      {
        title: "评论",
        id: 2
      },
      {
        title: "退款/售后",
        id: 3
      }
    ],
    orderData: [],
    yi: [],
    wei: [],
    hou: [],
    ping:[]
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    this.setData({
      num: options.id
    })
    this.getOrderList()
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {
    this.getOrderList()
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  },

  // 改变
  changeType: function (e) {
    this.setData({
      num: e.currentTarget.dataset.id
    })
  },
  tabChange: function (e) {
    this.setData({
      num: e.detail.current
    })
    this.orderShow()
  },
  orderShow: function () {
    let that = this
    switch (this.data.num) {
      case 0: //flase
        that.waitPayShow()
        break
      case 1: //true
        that.alreadyShow()
        break
      case 3:
        that.lostShow()
        break
    }
  },
  // 订单
  async getOrderList(params) {
    await request('/users/', params, {}, 'GET').then(res => {
      this.setData({
        orderData: res
      })
      this.orderShow()
    })
  },
  // 已付款
  alreadyShow: function () {
    let data = this.data.orderData.filter(item => {
      return item.orderState === "已支付"
    })
    this.setData({
      yi: data
    })
  },
  //  未付款
  waitPayShow: function () {
    let data = this.data.orderData.filter(item => {
      return item.orderState === "未支付"
    })
    this.setData({
      wei: data
    })
  },
  //  售后
  lostShow: function () {
    let data = this.data.orderData.filter(item => {
      return item.orderState === "已退款"
    })
    this.setData({
      hou: data
    })
  },
  // 更改订单
  changeOrder: function (e) {
    let that = this
    let data = e.currentTarget.dataset.item.userTel
    wx.showModal({
      title: '支付',
      content: '确定要支付吗',
      success(res) {
        if (res.confirm) {
          that.getNew(data)
        } else if (res.cancel) {
          console.log('用户点击取消')
        }
      }
    })
  },
  async getNew(params) {
    console.log(params);
    let that = this
    await request('/users/changeData', {
      tel: params
    }, {
      "Content-Type": "application/x-www-form-urlencoded"
    }, 'POST').then(res => {
      console.log(res, '新数据');
      that.getOrderList()
    })
  },
  // 删除订单
  deleteData(e) {
    let id = e.currentTarget.dataset.id
    wx.showModal({
      title: '支付',
      content: '确定要支付吗',
      success(res) {
        if (res.confirm) {
          request('/users/deleteData', {
            tel: id
          }, {
            "Content-Type": "application/x-www-form-urlencoded"
          }, 'POST').then(res => {
            console.log(res, '新数据');
            this.getOrderList()
          })
        } else if (res.cancel) {
          console.log('用户点击取消')
        }
      }
    })
  }
})